import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login'
import Home from '../views/Home'
import Welcome from '../views/Welcome'
import User from '../views/users/User'
import Rights from '../views/power/Rights'
import Roles from '../views/power/Roles'
import Cate from '../views/goods/Cate'
import Params from '../views/goods/Params'
import Goods from '../views/goods/Goods'
import Orders from '../views/order/Orders'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect:'/welcome',
    children:[
    {path:'/welcome', name:'Welcome',component:Welcome},
    {path:'/users',name:'User',component:User},
    {path:'/rights',name:'rights',component:Rights},
    {path:'/roles',name:'roles',component:Roles},
    {path:'/categories',name:'cate',component:Cate},
    {path:'/params',name:'params',component:Params},
    {path:'/goods',name:'goods',component:Goods},
    {path:'/orders',name:'orders',component:Orders}
    ]
  }
  
]
//挂载路由导航守卫


const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  if(to.path ==='/login'){
    next()
  }
  //获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr){
    next('/login')
  }
  next()
})

export default router
